package com.shujia.mr.writable;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 需求：
 *      需要将student.txt文件读取，在Map端将数据包装成一个student对象再写出到Reduce端
 *
 * Mapper端：
 *      读取一行数据，将数据切分，创建学生对象对数据进行包装，
 *          再将学生编号作为Key 学生对象作为Value写出到Reduce端
 *
 * Reducer端:
 *      直接将对象作为Value,Null作为Key写出到文件中
 *
 */
public class MyWritableMapper extends Mapper<LongWritable, Text,Text,Student> {

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Student>.Context context) throws IOException, InterruptedException {
        // 1500100001,施笑槐,22,女,文科六班
        String[] columns = value.toString().split(",");
        Student student = new Student(columns[0], columns[1], columns[2], columns[3], columns[4]);
        // 将学生ID和学生对象写出
        context.write(new Text(columns[0]),student);
    }
}
